﻿
using AjaxControlToolkit;
using FarPoint.Web.Spread;
using FarPoint.Web.Spread.Extender;
using FarPoint.Web.Spread.Renderer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.CellTypes
{
  public partial class Overview : SpreadDemoPage
  {
    protected void Page_Load(object sender, System.EventArgs e)
    {
      if (!Page.IsPostBack)
      {
        this.FpSpread1.UseClipboard = false;
        FpSpread1.Sheets.Count = 2;
        FpSpread1.Sheets[0].SheetName = "普通类型";
        FpSpread1.Sheets[1].SheetName = "Ajax类型";
        InitCellTypes(FpSpread1.Sheets[0]);
        InitExtenderSheet(FpSpread1.Sheets[1]);
      }
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {

    }
    #endregion

    private void InitCellTypes(SheetView sheet)
    {
      sheet.AllowPage = false;
      sheet.SetColumnWidth(2, 250);
      sheet.PageSize = 15;
      sheet.AlternatingRows[0].BackColor = Color.Linen;
      sheet.ColumnHeader.AutoText = HeaderAutoText.Blank;
      sheet.ColumnHeader.Cells[0, 0].Text = "单元格类型";
      sheet.ColumnHeader.Cells[0, 2].Text = "示例";

      FpSpread1.HorizontalScrollBarPolicy = ScrollBarPolicy.Never;

      FarPoint.Web.Spread.NamedStyle nstyledata = new FarPoint.Web.Spread.NamedStyle("StyleData");
      //Define the styles for the Data portion
      nstyledata.Font.Name = "Verdana";
      nstyledata.Font.Size = FontUnit.XSmall;
      nstyledata.Parent = "DataAreaDefault";
      sheet.DefaultStyle.Parent = "StyleData";
      FpSpread1.NamedStyles.Add(nstyledata);

      FpSpread1.Columns[0].Font.Bold = true;
      FpSpread1.Columns[0].Font.Name = "Verdana";
      FpSpread1.Columns[0].Font.Size = FontUnit.XSmall;

      FpSpread1.Columns[0].Width = 125;
      FpSpread1.Columns[1].Width = 75;

      FpSpread1.ColumnHeader.DefaultStyle.Border.BorderStyle = BorderStyle.None;
      FpSpread1.ColumnHeader.DefaultStyle.BackColor = Color.IndianRed;
      FpSpread1.ColumnHeader.DefaultStyle.ForeColor = Color.White;
      FpSpread1.ColumnHeader.DefaultStyle.Font.Bold = true;
      FpSpread1.RowHeader.Visible = false;
      UseHelperObjects(sheet);
    }

    public void UseHelperObjects(SheetView sheet)
    {
      sheet.Cells[0, 0].Text = "FarPoint";

      sheet.RowCount = 18;
      sheet.ColumnCount = 4;

      //Add Data
      sheet.Cells[0, 0].Text = "标签单元格类型:";
      sheet.Cells[0, 2].Text = "标签";

      sheet.Cells[1, 0].Text = "按钮单元格类型:";

      sheet.Cells[2, 0].Text = "图片单元格类型:";
      sheet.Cells[2, 2].Text = "使用 ImageAlign 属性定制图片和文本相对位置。";

      sheet.Cells[3, 0].Text = "列表单元格类型:";
      sheet.Cells[3, 2].Text = "0";

      sheet.Cells[4, 0].Text = "复选框单元格类型:";
      sheet.Cells[4, 2].Value = true;

      sheet.Cells[5, 0].Text = "单选框单元格类型:";
      sheet.Cells[5, 2].Text = "1";

      sheet.Cells[6, 0].Text = "超链接单元格类型:";
      sheet.Cells[6, 2].Text = "葡萄城控件官方网站";

      sheet.Cells[7, 0].Text = "下拉框单元格类型:";
      sheet.Cells[7, 2].Text = "红色";

      sheet.Cells[8, 0].Text = "数值单元格类型:";
      sheet.Cells[8, 2].Text = "123";

      sheet.Cells[9, 0].Text = "日期单元格类型:";
      sheet.Cells[9, 2].Value = DateTime.Now.ToOADate();

      sheet.Cells[10, 0].Text = "双精度单元格类型:";
      sheet.Cells[10, 2].Text = "123.45";

      sheet.Cells[11, 0].Text = "货币单元格类型:";
      sheet.Cells[11, 2].Text = "12345.01";

      sheet.Cells[12, 0].Text = "文本单元格类型:";
      sheet.Cells[12, 2].Text = "text";

      sheet.Cells[13, 0].Text = "正则单元格类型:";
      sheet.Cells[13, 2].Text = "123-45-6789";

      sheet.Cells[14, 0].Text = "普通单元格类型:";
      sheet.Cells[14, 2].Text = "普通单元格";

      sheet.Cells[15, 0].Text = "百分比单元格类型:";
      sheet.Cells[15, 2].Text = ".69";

      sheet.Cells[16, 0].Text = "多列单元格类型:";

      sheet.Cells[17, 0].Text = "标签云单元格类型:";

      //Lock the specified column cells from being edited
      //Columns 0 and 1
      sheet.Columns[0, 1].HorizontalAlign = HorizontalAlign.Right;
      sheet.Columns[0, 1].CellType = new LabelCellType();
      sheet.Columns[0, 1].Locked = true;

      //Column 3
      sheet.Columns[3].HorizontalAlign = HorizontalAlign.Right;
      sheet.Columns[3].CellType = new LabelCellType();
      sheet.Columns[3].Locked = true;

      //Unlock the button example cells
      sheet.Cells[1, 1, 1, 3].Locked = false;

      //Add Cell Types
      //Label
      sheet.Cells[0, 2].CellType = new LabelCellType();

      //Button cell
      //Image button
      sheet.Cells[1, 1].CellType = new ButtonCellType("MyCommand", FarPoint.Web.Spread.ButtonType.ImageButton, IMAGE_FOLDER + "/buttoncell.gif");
      sheet.Rows[1].Height = 24;
      //Push button
      sheet.Cells[1, 2].CellType = new ButtonCellType("MyCommand", FarPoint.Web.Spread.ButtonType.PushButton, "Click");
      //Link button
      sheet.Cells[1, 3].CellType = new ButtonCellType("MyCommand", FarPoint.Web.Spread.ButtonType.LinkButton, "Click");
      sheet.Cells[1, 3].HorizontalAlign = HorizontalAlign.Center;

      //Image cell     
      sheet.Cells[2, 2].CellType = new ImageCellType(IMAGE_FOLDER + "/english.gif");
      sheet.Rows[2].Height = 40;

      //ListBox cell              
      sheet.Cells[3, 2].CellType = new ListBoxCellType(new string[] { "Red", "Green", "Blue" }, new string[] { "Red", "Green", "Blue" });
      sheet.Rows[3].Height = 60;

      //Checkbox cell type
      sheet.Cells[4, 2].CellType = new CheckBoxCellType();

      //Radio button list
      sheet.Cells[5, 2].CellType = new RadioButtonListCellType(new string[] { "Red", "Green", "Blue" }, new string[] { "Red", "Green", "Blue" });

      //HyperLink cell 
      sheet.Cells[6, 2].CellType = new HyperLinkCellType("http://www.gcpowertools.com.cn/");

      //Combobox
      sheet.Cells[7, 2].CellType = new ComboBoxCellType(new string[] { "Red", "Green", "Blue" }, new string[] { "Red", "Green", "Blue" });

      //Integer
      IntegerCellType fpcell = new IntegerCellType();
      fpcell.ErrorMessage = "Invalid Integer (123)";
      sheet.Cells[8, 2].CellType = fpcell;

      //Date
      sheet.Cells[9, 2].CellType = new DateTimeCellType();

      //Double
      sheet.Cells[10, 2].CellType = new DoubleCellType();

      //Currency
      sheet.Cells[11, 2].CellType = new CurrencyCellType();

      //RegExp
      RegExpCellType rg = new RegExpCellType();
      rg.ErrorMessage = "SSN ( ex, 123-45-6789 )";
      rg.ValidationExpression = "^\\d{3}-\\d{2}-\\d{4}$";
      sheet.Cells[13, 2].CellType = rg;

      sheet.Cells[14, 2].CellType = new GeneralCellType();

      //Add cell span for the radio button cell
      //sheet.Cells[5, 2].Text = "1";
      sheet.SpanModel.Add(5, 2, 1, 2);

      //Percent
      sheet.Cells[15, 2].CellType = new PercentCellType();

      sheet.Cells[16, 2].CellType = CreateMCCBCellType();

      sheet.Cells[17, 2].CellType = CreateTagClouCellType();
    }

    private MultiColumnComboBoxCellType CreateMCCBCellType()
    {
      MultiColumnComboBoxCellType mccbct1 = new MultiColumnComboBoxCellType();
      DataTable table = new DataTable("Demo");
      table.Columns.Add("ID", typeof(int));
      table.Columns.Add("Name", typeof(string));
      table.Columns.Add("University", typeof(string));

      table.Rows.Add(new object[] { 0, "Owen", "NWPU" });
      table.Rows.Add(new object[] { 1, "Winnow", "XJTU" });
      table.Rows.Add(new object[] { 2, "Venture", "NWPU" });
      table.Rows.Add(new object[] { 3, "Lucky", "XDU" });

      mccbct1.DataSource = table;

      mccbct1.DataColumn = 1;
      mccbct1.ColumnEditName = "University";
      mccbct1.ShowButton = true;
      mccbct1.ListWidth = 300;
      mccbct1.ColumnWidths = new int[] { 40, 100, 100 };

      return mccbct1;
    }

    private TagCloudCellType CreateTagClouCellType()
    {
      TagCloudCellType tagCloudCellType = new TagCloudCellType();
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem(".net", 39));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("2005", 16));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("access", 17));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("ajax", 18));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("api", 32));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("asp.net", 45));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("bug", 100));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("constant", 211));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("c#", 85));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("css", 26));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("dhtml", 42));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("error", 150));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("excel", 36));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("framework", 16));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("outlook", 22));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("script", 15));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("sdk", 18));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("sharepoint", 216));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("sql", 66));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("unicode", 36));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("vb.net", 24));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("vista", 46));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("web", 333));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("webpart", 6));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("windows", 116));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("wmi", 56));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("workflow", 17));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("wpf", 76));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("xml", 34));
      tagCloudCellType.TagCloudItems.Add(CreateTagCloudItem("window 8", 534));

      tagCloudCellType.RankingColors = new string[] { "black", "cyan", "green", "orange", "pink", "red", "purple" };
      tagCloudCellType.RankingFonts = new string[] { "Arial", "Century", "High Tower Text", "Georgia", "Onyx", "Batang", "Times new roman" };
      return tagCloudCellType;
    }

    private TagCloudItem CreateTagCloudItem(string name, int weight)
    {
      return new TagCloudItem(name, weight, "http://msdn.microsoft.com/en-us/library/Tags-Cloud.aspx?tag=" + name, weight.ToString() + " item(s) tagged");
    }

    protected void InitExtenderSheet(SheetView sheet)
    {
      sheet.ColumnCount = 5;
      sheet.RowCount = 8;

      sheet.Columns[0].Font.Size = FontUnit.Point(10);
      sheet.Columns[1].Font.Size = FontUnit.Point(10);
      sheet.Columns[2].Font.Size = FontUnit.Point(10);
      sheet.Columns[3].Font.Size = FontUnit.Point(10);
      sheet.Columns[4].Font.Size = FontUnit.Point(10);
      //sheet.Columns[5].Font.Size = FontUnit.Point(10);

      sheet.Cells[0, 0].Text = "掩码日期输入单元格类型 -->";
      sheet.Columns[0].Width = 180;

      sheet.Cells[0, 0].ForeColor = Color.IndianRed;
      sheet.Cells[0, 0].BackColor = Color.MintCream;

      FarPoint.Web.Spread.Extender.DateCalendarCellType dc = new FarPoint.Web.Spread.Extender.DateCalendarCellType();
      MaskedEditExtender mee = new MaskedEditExtender();
      mee.Mask = "9999/99/99";
      mee.MaskType = MaskedEditType.Date;
      mee.UserDateFormat = MaskedEditUserDateFormat.YearMonthDay;
      dc.DateFormat = "yyyy/MM/dd";
      dc.Extenders.Add(mee);
      sheet.Cells[0, 1].CellType = dc;
      sheet.Cells[0, 1].BackColor = Color.MintCream;
      sheet.Cells[0, 1].Value = DateTime.Today;

      sheet.Cells[1, 0].Text = "掩码编辑单元格类型 -->";
      sheet.Cells[1, 0].ForeColor = Color.IndianRed;
      sheet.Cells[1, 0].BackColor = Color.Snow;
      MaskedEditCellType ext2 = new MaskedEditCellType();
      ext2.Editor.BackColor = Color.Snow;
      ext2.Editor.BorderWidth = 0;
      ext2.Converters.Add(new CustomConverter());
      ext2.ShowEditor = true;
      ext2.MaskType = MaskedEditType.Number;
      ext2.Mask = "9,999,999.99";
      ext2.DisplayMoney = MaskedEditShowSymbol.Left;
      ext2.ClearMaskOnLostFocus = true;
      sheet.Cells[1, 1].CellType = ext2;
      sheet.Cells[1, 1].Column.Width = 100;
      sheet.Cells[1, 1].BackColor = Color.Snow;

      sheet.Cells[2, 0].Text = "自动完成单元格类型 -->";
      sheet.Cells[2, 0].ForeColor = Color.IndianRed;
      sheet.Cells[2, 0].BackColor = Color.Honeydew;

      AutoCompleteCellType ext6 = new AutoCompleteCellType();
      ext6.ShowEditor = true;
      TextBoxWatermarkExtender twe = new TextBoxWatermarkExtender();
      twe.WatermarkText = "输入字母";
      twe.WatermarkCssClass = "水印";
      ext6.Extenders.Add(twe);
      ext6.ServiceMethod = "GetCompletionList";
      ext6.MinimumPrefixLength = 1;
      ext6.CompletionInterval = 1000;
      ext6.EnableCaching = true;
      ext6.CompletionSetCount = 10;
      ext6.DelimiterCharacters = ";, :";
      sheet.Cells[2, 1].Column.Width = 110;
      sheet.Cells[2, 1].BackColor = Color.Honeydew;
      sheet.Cells[2, 1].CellType = ext6;

      sheet.Cells[0, 2].Text = "滑动条单元格类型 -->";
      sheet.Cells[0, 2].ForeColor = Color.IndianRed;
      sheet.Cells[0, 2].BackColor = Color.Ivory;

      SliderCellType ext3 = new SliderCellType();
      ext3.EnableHandleAnimation = true;
      ext3.Minimum = 0;
      ext3.Maximum = 100;
      ext3.TooltipText = "{0}";
      sheet.Cells[0, 3].CellType = ext3;
      sheet.Cells[0, 3].Value = 20;
      sheet.Cells[0, 3].BackColor = Color.Ivory;

      sheet.Cells[1, 2].Text = "数值输入单元格类型";
      sheet.Cells[1, 2].ForeColor = Color.IndianRed;
      sheet.Cells[1, 2].BackColor = Color.Cornsilk;
      NumericUpDownCellType ext5 = new NumericUpDownCellType();
      ext5.ShowEditor = true;
      ext5.Minimum = 0;
      ext5.Maximum = 100;
      ext5.Step = 5;
      ext5.Width = 129;
      sheet.Cells[1, 3].CellType = ext5;
      sheet.Cells[1, 3].Column.Width = 130;
      sheet.Cells[1, 3].BackColor = Color.Cornsilk;

      sheet.Cells[3, 0].Text = "星级单元格类型 -->";
      sheet.Cells[3, 0].ForeColor = Color.IndianRed;
      RatingCellType ext4 = new RatingCellType();
      ext4.StarCssClass = "ratingStar";
      ext4.EmptyStarCssClass = "emptyRatingStar";
      ext4.FilledStarCssClass = "filledRatingStar";
      ext4.WaitingStarCssClass = "savedRatingStar";
      ext4.CurrentRating = 2;
      ext4.MaxRating = 5;
      sheet.Cells[3, 1].CellType = ext4;

      sheet.Cells[4, 0].Text = "互斥复选框单元格类型 -->";
      sheet.Cells[4, 0].ForeColor = Color.IndianRed;
      sheet.Columns[0].Width = 230;
      MutuallyExclusiveCheckBoxCellType dt1 = new MutuallyExclusiveCheckBoxCellType();
      dt1.Text = "复选框1";
      dt1.Key = "Key1";
      sheet.Cells[4, 1].CellType = dt1;
      sheet.Cells[4, 1].Value = true;
      MutuallyExclusiveCheckBoxCellType dt2 = new MutuallyExclusiveCheckBoxCellType();
      dt2.Text = "复选框2";
      dt2.Key = "Key1";
      sheet.Columns[1].Width = 160;
      sheet.Cells[4, 2].CellType = dt2;
      MutuallyExclusiveCheckBoxCellType dt11 = new MutuallyExclusiveCheckBoxCellType();
      dt11.Text = "复选框3";
      dt11.Key = "Key2";
      sheet.Columns[2].Width = 120;
      sheet.Cells[4, 3].CellType = dt11;
      MutuallyExclusiveCheckBoxCellType dt12 = new MutuallyExclusiveCheckBoxCellType();
      dt12.Text = "复选框4";
      dt12.Key = "Key2";
      sheet.Cells[4, 4].CellType = dt12;
      sheet.Columns[3].Width = 120;
      sheet.Columns[4].Width = 100;

      sheet.Cells[5, 0].Text = "幻灯片单元格类型 -->";
      sheet.Cells[5, 0].ForeColor = Color.IndianRed;
      sheet.Cells[5, 0].ColumnSpan = 2;
      SlideShowCellType ssct = new SlideShowCellType();
      ssct.AutoPlay = true;
      ssct.Loop = true;
      ssct.PlayInterval = 1000;
      ssct.SlideShowServiceMethod = "GetSlides";
      sheet.Cells[5, 2].CellType = ssct;
      sheet.Cells[5, 2].ColumnSpan = 2;
      sheet.Cells[5, 2].Row.Height = sheet.Columns[3].Width + sheet.Columns[3].Width;
    }

    private static AccessDataSource ds = new AccessDataSource("../../App_Data/Extenders.mdb", "SELECT Extenders FROM Extenders");

    [WebMethod]
    [ScriptMethod]
    public static string[] GetCompletionList(string prefixText, int count)
    {
      List<string> items = new List<string>();
      foreach (object obj in ds.Select(DataSourceSelectArguments.Empty))
      {
        DataRowView dr = obj as DataRowView;
        string str = dr["Extenders"].ToString();
        if (str != null && str.StartsWith(prefixText, StringComparison.OrdinalIgnoreCase))
        {
          if (!items.Contains(str))
            items.Add(str);
        }
        if (items.Count == count)
          break;
      }
      return items.ToArray();
    }

    [WebMethod]
    [ScriptMethod]
    public static Slide[] GetSlides()
    {
      List<Slide> items = new List<Slide>();
      items.Add(new Slide("../images/gnome-applications.png", "", ""));
      items.Add(new Slide("../images/gnome-graphics.png", "", ""));
      items.Add(new Slide("../images/gnome-joystick.png", "", ""));
      items.Add(new Slide("../images/gnome-reboot.png", "", ""));
      items.Add(new Slide("../images/gnome-shutdown.png", "", ""));
      items.Add(new Slide("../images/gnome-window-manager.png", "", ""));
      items.Add(new Slide("../images/hwbrowser.png", "", ""));
      items.Add(new Slide("../images/palm-pilot-sync.png", "", ""));
      items.Add(new Slide("../images/panel-notification-area.png", "", ""));
      items.Add(new Slide("../images/panel-window-list.png", "", ""));
      items.Add(new Slide("../images/sodipodi.png", "", ""));
      items.Add(new Slide("../images/system-floppy.png", "", ""));
      items.Add(new Slide("../images/text-editor.png", "", ""));
      items.Add(new Slide("../images/demo-cover.png", "", ""));
      items.Add(new Slide("../images/v4_title.gif", "", ""));
      items.Add(new Slide("../images/body_bg.gif", "", ""));
      return items.ToArray();
    }

    public class CustomConverter : JavaScriptConverter
    {
      public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
      {
        if (type.Equals(typeof(TextBox)))
        {
          TextBox tb = new TextBox();
          tb.BackColor = Color.FromArgb((int)dictionary["BackColor"]);
          tb.BorderWidth = (Unit)dictionary["BorderWidth"];
          return tb;
        }
        else if (type.Equals(typeof(Unit)))
        {
          Unit u = new Unit(Convert.ToDouble(dictionary["Value"]), (UnitType)dictionary["Type"]);
          return u;
        }
        return null;
      }

      public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
      {
        Dictionary<string, object> dict = new Dictionary<string, object>();
        TextBox tb = obj as TextBox;
        if (tb != null)
        {
          dict.Add("BackColor", tb.BackColor.ToArgb());
          dict.Add("BorderWidth", tb.BorderWidth);
        }
        else if (obj is Unit)
        {
          Unit u = (Unit)obj;
          dict.Add("Value", u.Value);
          dict.Add("Type", u.Type);
        }
        return dict;
      }

      public override IEnumerable<Type> SupportedTypes
      {
        get { return new Type[] { typeof(TextBox), typeof(Unit) }; }
      }
    }
  }
}